package ar.com.spsolutions.splibrary.services;

import java.util.List;

import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;

import ar.com.spsolutions.splibrary.author.Author;
import ar.com.spsolutions.splibrary.general.IFindByNameService;
import ar.com.spsolutions.splibrary.general.RoleConverterService;
import ar.com.spsolutions.splibrary.repositories.AuthorRepository;

public class AuthorService extends TemplateEntityService<Author> implements
		IFindByNameService<Author> {

	private static final long serialVersionUID = 5335404074747257761L;

	@Override
	@Transactional(readOnly = true)
	public List<Author> findByLikeName(final String name) {
		return this.getRepository().findByLike("name", name);
	}

	@Override
	@Secured(value = { RoleConverterService.ROLE_LIBRARIAN })
	public void add(final Author aEntity) {
		super.add(aEntity);
	}

	@Transactional(readOnly = true)
	public Author findByName(final String name) {
		return ((AuthorRepository) this.getRepository()).findByName(name);
	}

	public boolean exist(final Author authorModel) {
		return !(((AuthorRepository) this.getRepository())
				.findByName(authorModel.getName()) == null);
	}

	@Transactional(readOnly = true)
	public int sizeOfSearch(final String searchValue) {
		return this.getMyRepository().sizeOfSearch(searchValue);
	}

	private AuthorRepository getMyRepository() {
		return (AuthorRepository) this.getRepository();
	}

	@Transactional(readOnly = true)
	public List<? extends Author> getResults(final String searchValue,
			final int first, final int count) {
		return this.getMyRepository().getResults(searchValue, first, count);
	}

}
